home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / µSim 1.0.5 / FabLibsƒ / Independents.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-23  |  5.1 KB  |  272 lines  |  [TEXT/CWIE]

  1. #include    "Independents.h"
  2.  
  3.  
  4. StringPtr GetPtrIndHString(Handle resH, unsigned short index)
  5. {
  6. unsigned short    *compPtr = (unsigned short *)*resH;
  7. unsigned char    *spanPtr;
  8. unsigned short    i = index;
  9.  
  10. if (*compPtr++ <= i)
  11.     spanPtr = nil;
  12. else {
  13.     spanPtr = (unsigned char *)compPtr;
  14.     while (i) {
  15.         spanPtr += *spanPtr++;
  16.         i--;
  17.         }
  18.     }
  19.  
  20. return (StringPtr)spanPtr;
  21. }
  22.  
  23. void GetIndHString(StringPtr dest, Handle resH, unsigned short index)
  24. {
  25. unsigned short    *compPtr = (unsigned short *)*resH;
  26. unsigned char    *spanPtr;
  27. unsigned short    i = index;
  28.  
  29. if (*compPtr++ <= i)
  30.     StrLength(dest) = 0;
  31. else {
  32.     spanPtr = (unsigned char *)compPtr;
  33.     while (i) {
  34.         spanPtr += *spanPtr++;
  35.         i--;
  36.         }
  37.     BlockMoveData(spanPtr, dest, *spanPtr + 1L);
  38.     }
  39.  
  40. return;
  41. }
  42. /*
  43. short KeyState(unsigned short k )
  44. // k =  any keyboard scan code, 0-127
  45. {
  46. KeyMap    km;
  47.  
  48. GetKeys(km);
  49. return ( ( *((unsigned char *)km + (k>>3)) >> (k & 7) ) & 1);
  50. }
  51. */
  52.  
  53. Boolean ModifiersState(short mask)
  54. {
  55. EventRecord    ev;
  56.  
  57. (void) EventAvail(0, &ev);
  58. return (ev.modifiers & mask) != 0;
  59. }
  60.  
  61. OSType Str2OSType(ConstStr255Param theStr)
  62. {
  63. OSType result;
  64. Ptr    source, dest;
  65. unsigned char i = StrLength(theStr);
  66.  
  67. source = (Ptr)theStr + 1;
  68. dest = (Ptr)&result;
  69. *dest++ = *source++;
  70. *dest++ = *source++;
  71. *dest++ = *source++;
  72. *dest++ = *source++;
  73.  
  74. if (i < 4)
  75.     while (i < 4) {
  76.         *--dest = 0x20;
  77.         i++;
  78.         }
  79.  
  80. return result;
  81. }
  82.  
  83. void OSType2Str(OSType typ, StringPtr theStr)
  84. {
  85. register StringPtr    tempPtr = theStr;
  86. register StringPtr    sourcePtr = (StringPtr)&typ;
  87.  
  88. *tempPtr++ = 4;
  89. *tempPtr++ = *sourcePtr++;
  90. *tempPtr++ = *sourcePtr++;
  91. *tempPtr++ = *sourcePtr++;
  92. *tempPtr = *sourcePtr;
  93. }
  94.  
  95. Handle FabGetFullPath(const FSSpecPtr fss, Boolean AUXpresent)
  96. {
  97. CInfoPBRec    mypb;
  98. Str63    dirName;
  99. Handle    fullPath;
  100. Size    hCurSize;
  101. OSErr    err = noErr;
  102.  
  103. fullPath = NewHandle(StrLength(fss->name));
  104. if (fullPath) {
  105.     BlockMoveData(fss->name + 1, *fullPath, StrLength(fss->name));
  106.     if (fss->parID != fsRtParID) {
  107.         mypb.dirInfo.ioNamePtr = dirName;
  108.         mypb.dirInfo.ioVRefNum = fss->vRefNum;
  109.         mypb.dirInfo.ioDrParID = fss->parID;
  110.         mypb.dirInfo.ioFDirIndex = -1;
  111.         do {
  112.             mypb.dirInfo.ioDrDirID = mypb.dirInfo.ioDrParID;
  113.             err = PBGetCatInfoSync(&mypb);
  114.             if (err)
  115.                 break;
  116.             else {
  117.                 if (AUXpresent) {
  118.                     if (dirName[1] != '/') {
  119.                         StrLength(dirName) += 1;
  120.                         dirName[StrLength(dirName)] = '/';
  121.                         }
  122.                     }
  123.                 else {
  124.                     StrLength(dirName) += 1;
  125.                     dirName[StrLength(dirName)] = ':';
  126.                     }
  127.                 hCurSize = InlineGetHandleSize(fullPath);
  128.                 SetHandleSize(fullPath, hCurSize + StrLength(dirName));
  129.                 err = MemError();
  130.                 if (err)
  131.                     break;
  132.                 else {
  133.                     BlockMoveData(*fullPath, *fullPath + StrLength(dirName), hCurSize);
  134.                     BlockMoveData(dirName + 1, *fullPath, StrLength(dirName));
  135.                     }
  136.                 }
  137.             }
  138.         while (mypb.dirInfo.ioDrDirID != fsRtDirID);
  139.         }
  140.     }
  141. if (err) {
  142.     DisposeHandle(fullPath);
  143.     fullPath = nil;
  144.     }
  145. return fullPath;
  146. }
  147.  
  148. void fabc2pstr(unsigned char *cs, StringPtr destps)
  149. {
  150. unsigned char *scanPtr = cs;
  151. unsigned char *destPtr = destps + 1;
  152. unsigned int count = 0;
  153.  
  154. while ((*destPtr++ = *scanPtr++) && count < UCHAR_MAX)
  155.     count++;
  156. StrLength(destps) = count;
  157. }
  158.  
  159. Boolean IsOnScreen(const RectPtr r)
  160. {
  161. Point    topRight;
  162.  
  163. topRight.h = r->right;
  164. topRight.v = r->top;
  165. return (PtInRgn(topLeft(*r), GetGrayRgn()) || (PtInRgn(topRight, GetGrayRgn())));
  166. }
  167.  
  168. Boolean IsOnScreenWeak(Point pt)
  169. {
  170. Rect    box;
  171.  
  172. topLeft(box) = pt;
  173. box.bottom = box.top + qd.thePort->portRect.bottom - qd.thePort->portRect.top;
  174. box.right = box.left + qd.thePort->portRect.right - qd.thePort->portRect.left;
  175. return IsOnScreen(&box);
  176. }
  177.  
  178. #pragma segment Init
  179.  
  180. void SetupStringFromPrefs(StringHandle *gString, short strID)
  181. {
  182. register Handle    tempH;
  183.  
  184. tempH = (Handle)GetString(strID);
  185. if (tempH) {
  186.     DetachResource(tempH);
  187.     if (*gString)
  188.         DisposeHandle((Handle)*gString);
  189.     *gString = (StringHandle)tempH;
  190.     }
  191. }
  192.  
  193. UInt32 SetupULongFromPrefs(short strID)
  194. {
  195. register Handle    tempH;
  196. UInt32    theCount = 0UL;
  197.  
  198. tempH = Get1Resource('ULNG', strID);
  199. if (tempH) {
  200.     theCount = *(UInt32 *)*tempH;
  201.     ReleaseResource(tempH);
  202.     }
  203. return theCount;
  204. }
  205.  
  206. Boolean SetupBooleanFromPrefs(short strID)
  207. {
  208. register Handle    tempH;
  209. Boolean    theCount = false;
  210.  
  211. tempH = Get1Resource('BOOL', strID);
  212. if (tempH) {
  213.     theCount = *(Boolean *)*tempH;
  214.     ReleaseResource(tempH);
  215.     }
  216. return theCount;
  217. }
  218.  
  219. #pragma segment CleanUp
  220.  
  221. void AddString(StringHandle theAddedStr, short strID)
  222. {
  223. register Handle tempH;
  224.  
  225. SetResLoad(false);
  226. tempH = Get1Resource('STR ', strID);
  227. SetResLoad(true);
  228. if (tempH) {
  229.     RemoveResource(tempH);
  230.     DisposeHandle(tempH);
  231.     }
  232. if (theAddedStr)
  233.     AddResource((Handle)theAddedStr, 'STR ', strID, "\p");
  234. }
  235.  
  236. void AddULong(UInt32 theAddedL, short strID)
  237. {
  238. register Handle tempH;
  239.  
  240. SetResLoad(false);
  241. tempH = Get1Resource('ULNG', strID);
  242. SetResLoad(true);
  243. if (tempH) {
  244.     RemoveResource(tempH);
  245.     DisposeHandle(tempH);
  246.     }
  247. tempH = NewHandle(sizeof(UInt32));
  248. if (tempH) {
  249.     *(UInt32 *)*tempH = theAddedL;
  250.     AddResource(tempH, 'ULNG', strID, "\p");
  251.     }
  252. }
  253.  
  254. void AddBoolean(Boolean theAddedL, short strID)
  255. {
  256. register Handle tempH;
  257.  
  258. SetResLoad(false);
  259. tempH = Get1Resource('BOOL', strID);
  260. SetResLoad(true);
  261. if (tempH) {
  262.     RemoveResource(tempH);
  263.     DisposeHandle(tempH);
  264.     }
  265. tempH = NewHandle(sizeof(Boolean));
  266. if (tempH) {
  267.     *(Boolean *)*tempH = theAddedL;
  268.     AddResource(tempH, 'BOOL', strID, "\p");
  269.     }
  270. }
  271.  
  272.